package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.AdminDTO;
import com.quanquan.store.request.AdminRequest;
import com.quanquan.store.service.AdminService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import web.response.ResultBody;

/**
 * @ClassName:AdminController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/15 14:52
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }


    /**
     * 创建核销账号
     *
     * @param request
     * @return
     */
    @PostMapping("/add_admin")
    public ResultBody addAdmin(@RequestBody AdminRequest request) {
        boolean flag = adminService.addAdmin(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "创建账号成功");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 后端校验账号
     *
     * @param request
     * @return
     */
    @PostMapping("/find_by_account")
    public ResultBody findByAccount(@RequestBody AdminRequest request) {
        boolean flag = adminService.findByAccount(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "暂无此账号信息",flag);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL", flag);
    }

    /**
     * 查看详情
     *
     * @param request
     * @return
     */
    @PostMapping("/find_by_id")
    public ResultBody findById(@RequestBody AdminRequest request) {
        AdminDTO adminDTO = adminService.findById(request);
        if (adminDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此账号信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", adminDTO);
    }

    /**
     * 修改密码
     *
     * @param request
     * @return
     */
    @PostMapping("/update_password")
    public ResultBody updatePassword(@RequestBody AdminRequest request) {
        boolean flag = adminService.updatePassword(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "修改密码成功");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 修改密码
     *
     * @param request
     * @return
     */
    @PostMapping("/update")
    public ResultBody update(@RequestBody AdminRequest request) {
        boolean flag = adminService.update(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "修改密码成功");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }

    /**
     * 多条件查询
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_manage")
    public ResultBody queryByCondition(@RequestBody AdminRequest request) {
        PageDTO<AdminDTO> adminDTOPageDTO = adminService.queryByCondition(request);
        if (null != adminDTOPageDTO && adminDTOPageDTO.getList().size() > 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), adminDTOPageDTO);
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());

    }

}
